package com.wink_172.library.utils

import android.content.Context
import android.util.DisplayMetrics
import android.view.WindowManager
import com.umeng.socialize.utils.DeviceConfigInternal


/**
 * Created by hcc on 16/8/4 21:18
 * 100332338@qq.com
 *
 *
 * 屏幕像素转换工具类
 */
object DisplayUtil {   @JvmStatic
    fun px2dp(context: Context?, pxValue: Float): Int {
    val displayMetrics = context?.resources?.displayMetrics
    return Math.round(pxValue / (displayMetrics!!.xdpi / DisplayMetrics.DENSITY_DEFAULT))
    }

    @JvmStatic
    fun dp2px(context: Context?, dipValue: Float): Int {
        val displayMetrics = context!!.resources.displayMetrics
        return Math.round(dipValue * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT))
            .toInt()
    }
    @JvmStatic
    fun px2sp(context: Context?, px: Float): Int {
        return Math.round(px / context!!.getResources().getDisplayMetrics().scaledDensity);

    }
    @JvmStatic
    fun sp2px(context: Context?, spValue: Float): Int {
        return Math.round(spValue * context!!.getResources().getDisplayMetrics().scaledDensity);
    }
    @JvmStatic
    fun getScreenWidth(context: Context?): Int {
        val dm = context!!.resources.displayMetrics
        return dm.widthPixels
    }
    @JvmStatic
    fun getScreenHeight(context: Context?): Int {
        val dm = context!!.resources.displayMetrics
        return dm.heightPixels
    }
    @JvmStatic
    fun getDisplayDensity(context: Context?): Float {
        val windowManager =
            context!!.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val displayMetrics = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(displayMetrics)
        return displayMetrics.density
    }

}